package com.rogo.openapi.flickr.core
{
	import com.rogo.oauth.OAuthCore;
	import com.rogo.oauth.OAuthKey;
	import com.rogo.oauth.URLParams;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	/**
	 * 
	 * @author Rogo
	 */	
	public class FlickrAPI
	{
		public function FlickrAPI()
		{
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const FLICKR_API_URL:String = 'http://api.flickr.com/services/rest';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		public var oauthKey:OAuthKey = FlickrKey.getInstance();
		
		private var loaderDictionary:Dictionary = new Dictionary();
		
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 获取一个签名的URLRequest
		 * 
		 * oauth_nonce
		 * oauth_timestamp
		 * oauth_consumer_key
		 * oauth_signature_method
		 * oauth_version
		 * oauth_signature
		 * 
		 * 默认生成上面6个参数 使用urlParams添加参数
		 * 
		 * @param method 请求方法
		 * @param url 请求路径
		 * @param urlParams 请求参数
		 * @param oauthKey 请求密钥
		 */			
		public function getSignatureUrlReq(method:String, 
										   url:String, 
										   urlParams:URLParams, 
										   oauthKey:OAuthKey):URLRequest
		{
			return OAuthCore.getSignatureUrlReq(method, url, urlParams, oauthKey)
		}
		
		/**
		 * 获取一个没有签名的URLRequest
		 * 
		 * @param method 请求方法
		 * @param url 请求路径
		 * @param urlParams 请求参数
		 * @return 
		 */	
		public function getUrlReq(method:String, 
								  url:String, 
								  urlParams:URLParams):URLRequest
		{
			return OAuthCore.getUrlReq(method, url, urlParams);
		}
		
		public function load(urlReq:URLRequest, 
							 resultHandler:Function = null,
							 errorHandler:Function = null):void
		{
			var handler:Object = {'resultHandler' : resultHandler, 
				'errorHandler' : errorHandler};
			
			var urlLoader:URLLoader = new URLLoader();
			loaderDictionary[urlLoader] = handler;
			urlLoader.addEventListener(Event.COMPLETE, urlLoader_completeHandler);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, urlLoader_errorHandler);
			urlLoader.load(urlReq);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function urlLoader_completeHandler(e:Event):void
		{
			var handler:Object = loaderDictionary[e.currentTarget];
			if (handler && handler.resultHandler)
			{
				handler.resultHandler(e);
				delete loaderDictionary[e.currentTarget];
			}
		}
		
		private function urlLoader_errorHandler(e:IOErrorEvent):void
		{
			var handler:Object = loaderDictionary[e.currentTarget];
			if (handler && handler.resultHandler)
			{
				handler.errorHandler(e);
				delete loaderDictionary[e.currentTarget];
			}
		}
		
	}
}